package com.jinzhi.jzweb.controller;


import java.net.URLEncoder;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.TrainStudentInfoDO;
import com.jinzhi.jzweb.pojo.CertificatesExportVo;
import com.jinzhi.jzweb.pojo.ExportStatisticsVo;
import com.jinzhi.jzweb.pojo.TrainStudentInfoExportVo;
import com.jinzhi.jzweb.service.TrainStudentInfoService;
import com.jinzhi.jzweb.utils.EasyPoiUtil;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import net.sf.json.JSONArray;

import com.jinzhi.common.utils.Result;
import com.jinzhi.jzapi.utils.HttpClientUtil;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2020-10-19 15:43:40 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/trainStudentInfo")
public class TrainStudentInfoController extends BaseController {
	@Autowired
	private TrainStudentInfoService trainStudentInfoService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:trainStudentInfo:trainStudentInfo")
	String TrainStudentInfo(){
	    return "jzweb/trainStudentInfo/trainStudentInfo";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:trainStudentInfo:trainStudentInfo")
	public Result<Page<TrainStudentInfoDO>> list(TrainStudentInfoDO trainStudentInfoDTO){
		// 查询列表数据
		Page<TrainStudentInfoDO> page = trainStudentInfoService.selectPage(getPage(TrainStudentInfoDO.class),
					trainStudentInfoService.convertToEntityWrapper().orderBy("id", false));
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
				TrainStudentInfoDO obj = (TrainStudentInfoDO) page.getRecords().get(i);
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:trainStudentInfo:add")
	String add(){
	    return "jzweb/trainStudentInfo/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:trainStudentInfo:edit")
	String edit(@PathVariable("id") Long id,Model model){
		TrainStudentInfoDO trainStudentInfo = trainStudentInfoService.selectById(id);
		model.addAttribute("trainStudentInfo", trainStudentInfo);
	    return "jzweb/trainStudentInfo/edit";
	}
	
	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:trainStudentInfo:add")
	public Result<String> save( TrainStudentInfoDO trainStudentInfo){
		trainStudentInfoService.insert(trainStudentInfo);
        return Result.ok();
	}
	
	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:trainStudentInfo:edit")
	public Result<String>  update( TrainStudentInfoDO trainStudentInfo){
		boolean update = trainStudentInfoService.updateById(trainStudentInfo);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:trainStudentInfo:remove")
	public Result<String>  remove( Long id){
		trainStudentInfoService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:trainStudentInfo:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		trainStudentInfoService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
	@Log("导出汇总信息")
	@GetMapping("/export")
	@ResponseBody
	@Transactional
	@RequiresPermissions("jzweb:trainStudentInfo:export")
	public void exportStatistics(HttpServletRequest request, HttpServletResponse response) throws Exception {
//		// 告诉浏览器用什么软件可以打开此文件
//		response.setHeader("content-Type", "application/vnd.ms-excel");
//		// 下载文件的默认名称
//		response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("培训学员信息汇总表", "UTF-8") + ".xls");
//		//编码
//		response.setCharacterEncoding("UTF-8");

		List<TrainStudentInfoDO> groupList = trainStudentInfoService.all();
		List<TrainStudentInfoExportVo> excelList = new ArrayList<TrainStudentInfoExportVo>();
		for(TrainStudentInfoDO info : groupList) {
			TrainStudentInfoExportVo excel = new TrainStudentInfoExportVo();
			excel.setType(info.getType());
			excel.setName(info.getName());
			excel.setIdNumber(info.getIdNumber());
			excel.setMobile(info.getMobile());
			byte[] imageFromNetByUrl = HttpClientUtil.getImageFromNetByUrl(info.getPicAddress());
			excel.setPicAddress(imageFromNetByUrl);
			excelList.add(excel);
		}

		EasyPoiUtil.exportExcel(excelList, "培训学员信息", "培训学员信息", TrainStudentInfoExportVo.class, "培训学员信息表", response);
	}
	
}
